import api from '@api'
const state = {
  message: {
    list: [],
    pageNum: 0,
    page: 1
  }
}

const getters = {
  ['getMessage'] (state) {
    return state.message
  }
}

const actions = {
  async ['getMessage'] ({commit}, page) {
    if(page = 1) {
      api.get('api/v1/UserInfo/messageAllRead.ashx');
      commit('clearMessage');
    }
    let config={
      CurrentPage: page,
      PageSize: 10,
      messageType: 99,
      mRead: ''
    }
    let {data: {messageList,pageNum, StatusCode}} = await api.get('api/v1/UserInfo/messageList.ashx', config)
    if(StatusCode === '200') {
      commit('addMessage', { message: messageList, pageNum:Number(pageNum), page});
    }
    return StatusCode;
  }
}

const mutations = {
  ['addMessage'] (state, {message, pageNum, page}) {
    state.message.list = state.message.list.concat(message);
    state.message.pageNum = pageNum;
    state.message.page = page;
  },
  ['clearMessage'] (state) {
    state.message = {
      list: [],
      pageNum: 0,
      page: 1
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}